package com.mitei.dao.abstractdao;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mitei.dao.beans.persistants.Bean;
import com.mitei.dao.beans.persistants.BeanMapper;
import com.mitei.dao.enumerations.QueryTypes;
import com.mitei.parametrage.xml.beans.DBConnectionInfos;
import com.mitei.parametrage.xml.connection.DatabaseConnectionManager;


public abstract class AbstractDAO {
	
	private Connection connection;

	public AbstractDAO () {
		
	}
	
	protected Connection createConnection() {
		try {
			DBConnectionInfos infos = DatabaseConnectionManager.getInstance();
			
			
			Class.forName(infos.getDriverName());
			Connection connection = DriverManager.getConnection(infos.getConnexionString(), infos.getUser(), infos.getPassword());
//			DriverManager.setLogStream(System.out) ;
			return connection;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	protected void closeAll(ResultSet rs, Statement stmt, Connection connection) throws SQLException {
		rs.close();
		stmt.close();
		connection.close();
	}
	
	protected void closeAll(Statement stmt, Connection connection) throws SQLException {
		stmt.close();
		connection.close();
	}
	
	protected ArrayList<Bean> DAOselect(String query) {
		this.connection = createConnection();
		ArrayList<Bean> res = new ArrayList<Bean>();
		Statement stmt;
		try {
			stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			
			while(rs.next()) {
				res.add(BeanMapper.resultSet2Bean(rs, query));
			}
			this.closeAll(rs, stmt, connection);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	protected void DAOupdate(Bean bean) {
		this.updates(bean, QueryTypes.UPDATE);
	}
	
	protected void DAOinsert(Bean bean) {
		this.updates(bean, QueryTypes.INSERT);
	}
	
	protected void DAOdelete(Bean bean) throws SQLException {
		this.updates(bean, QueryTypes.DELETE);
	}
	
	protected void updates(Bean bean, QueryTypes queryTypes) {
		this.connection = createConnection();
		Statement stmt;
		try {
			stmt = connection.createStatement();
			String query = BeanMapper.bean2Query(bean, queryTypes);
			stmt.executeUpdate(query);
			connection.commit();
			this.closeAll(stmt, connection);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}
